package com.klun.concTrueWar.five;

import java.util.HashSet;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

public class HiddenIterator {
    // @GuardedBy("this")
    private final Set<Integer> set = new HashSet<Integer>();

    public synchronized void add(Integer i){
        set.add(i);
    }
    public synchronized void remove(Integer i){
        set.remove(i);
    }
    // 此方法 可能会抛出 ConcurrentModificationException
    // 因为 set.toString 进行了set迭代
    public void addTenThings(){
        Random r = new Random();
        for (int i = 0; i < 10; i++) {
            add(r.nextInt());
        }
        System.out.println("DEBUG added ten elements to " + set);
    }
    ConcurrentMap m = new ConcurrentHashMap();
}
